/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Author: Nicolas Longuet Marx
Date: June 20 2020
Last modified: Aug 19 2023
Object: Produce tables of the main paper
Databases in input: A_experiment.dta
Databases in output: None

Tables generated in this script:

- Table 2: Impact on overall satisfaction with the head of government
- Table 3: Impact on satisfaction with democracy
- Table 4: Impact on support for democratic ideals

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ****/

use "1_data/A_experiment.dta", replace

/* program to export estimation results better */
qui do "2_code/mylincom_program.do"
qui do "2_code/get_mean_program.do"
qui do "2_code/mylincom_program_2sls.do"


/* CONTROLS AND MISSING VALUES */
global controls "thirties fourties fifties sixties seventies income2quartile income3quartile income4quartile incomenoanswer female highschool college noreligion christiannotcatholic catholic fulltimeworker parttimeworker unemployed selfemployed outofLF goodhealth white black latino asian whitecollar bluecollar serviceworker"


global mv_controls
foreach y of global controls {
	global mv_controls $mv_controls mv_`y'
	gen mv_`y' = (`y' == .)
	replace `y' = 0 if `y' == .
}

foreach var of varlist $controls $mv_controls  {
	assert `var'!= .
}


* Check that treatment is well defined
foreach var of varlist blame* praisi* bad_* TH* TE* treatc* healthc* econc* {
	di "`var'"
	assert `var'!= .
}
	
la var eval_eco "Economic satisfaction"
la var eval_sant "Health satisfaction"	

**# Table 2: Impact on overall satisfaction with the head of government 

/* 2. Col 1. 16 Instruments */
eststo clear
eststo: ivreg2 satis_head ///
	(eval_eco eval_sant = TH1_TE1 TH1_TE2 TH1_TE3 TH1_TE4 TH2_TE1 TH2_TE2 TH2_TE3 TH2_TE4 TH3_TE1 TH3_TE2 TH3_TE3 TH3_TE4 TH4_TE1 TH4_TE2 TH4_TE3), small  robust

* Note: here and later, we report the Cragg-Donald statistic from the ivreg2 regression. We are following the ivreg2 helper and take the correspondingly-robust Kleibergen-Paap since we are using the robust command. 
estadd scalar fstat = e(widstat)
mylincom_2sls
estadd local iv "16 IVs"
get_mean

/* 2. Col 2. 16 Instruments + controls */
eststo: ivreg2 satis_head ///
	(eval_eco eval_sant = TH1_TE1 TH1_TE2 TH1_TE3 TH1_TE4 TH2_TE1 TH2_TE2 TH2_TE3 TH2_TE4 TH3_TE1 TH3_TE2 TH3_TE3 TH3_TE4 TH4_TE1 TH4_TE2 TH4_TE3) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
mylincom_2sls
estadd local iv "16 IVs"
estadd local CFE "X"
estadd local Controls "X", replace
get_mean

/* 2. Col 3. 2 Instruments */
eststo: ivreg2 satis_head ///
	(eval_eco eval_sant = healthc econc) , small robust 

estadd scalar fstat = e(widstat)
mylincom_2sls
estadd local iv "2 SumIVs"
get_mean

/* 2. Col 4. 2 Instruments + controls */
eststo: ivreg2 satis_head ///
	(eval_eco eval_sant = healthc econc) $controls $mv_controls  i.country, small robust 
	
estadd scalar fstat = e(widstat)
mylincom_2sls
estadd local iv "2 SumIVs"
estadd local CFE "X"
estadd local Controls "X", replace
get_mean

esttab using "3_output/1_main_paper/Tables/Table2.tex", depvar keep(eval_eco eval_sant ) ///
	order(eval_eco eval_sant ) label replace ///
	cells(b(star fmt(%15.3fc)) se(par fmt(%15.3fc))) interaction(" $/times$ ") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(Controls CFE N av nothing nothing2 diffeh diffehsd nothing iv fstat , layout(@ @ @ @ @ @ @ (@) @ @ @ ) fmt(%15s %15s %15.0fc %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %15s %9.3f ) ///
	labels("Individual controls" "Country FE" Observations "Outcome mean" " " "Linear combination of estimates:" "Difference Economic satisfaction" "-Health satisfaction" " " "Instruments" "Cragg-Donald statistic" )) ///
	collabels(none) mlabels(none) ///
	mgroups("Satisfaction with the head of government" , pattern(1 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
	

**# Table 3: Impact on satisfaction with democracy 
la var satis_head "Satisfaction with the head of government"
eststo clear
/* 3. Col 1: 16 instruments */

eststo: ivreg2 satis_dem ///
	(satis_head = TH1_TE1 TH1_TE2 TH1_TE3 TH1_TE4 TH2_TE1 TH2_TE2 TH2_TE3 TH2_TE4 TH3_TE1 TH3_TE2 TH3_TE3 TH3_TE4 TH4_TE1 TH4_TE2 TH4_TE3), small robust 

estadd scalar fstat = e(widstat)
estadd local IV "16 IVs"
estadd local nothing nothing2 = " "
get_mean

/* 3. Col 2: 16 instruments + controls */
eststo: ivreg2 satis_dem ///
	(satis_head = TH1_TE1 TH1_TE2 TH1_TE3 TH1_TE4 TH2_TE1 TH2_TE2 TH2_TE3 TH2_TE4 TH3_TE1 TH3_TE2 TH3_TE3 TH3_TE4 TH4_TE1 TH4_TE2 TH4_TE3) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local CFE "X", replace
estadd local Controls "X", replace
estadd local IV "16 IVs"
estadd local nothing nothing2 = " "
get_mean
	
/* 3. Col 3: 2 instruments */	
eststo: ivreg2 satis_dem ///
	(satis_head = healthc econc), small robust 

estadd scalar fstat = e(widstat)
estadd local IV "2 SumIVs"
get_mean


/* 3. Col 4: 2 instruments + controls */	
eststo: ivreg2 satis_dem ///
	(satis_head = healthc econc) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local IV "2 SumIVs"
estadd local Controls "X", replace
estadd local CFE "X", replace
get_mean


/* 3. Col 5: 1 instrument */	
eststo: ivreg2 satis_dem ///
	(satis_head = treatc), small robust 

estadd scalar fstat = e(widstat)
estadd local IV "SumIV"
estadd local nothing nothing2 = " "
get_mean

/* 3. Col 6: 1 instrument + controls */										
eststo: ivreg2 satis_dem ///
	(satis_head = treatc) $controls $mv_controls  i.country, small robust 
	
estadd scalar fstat = e(widstat)
estadd local IV "SumIV"
estadd local nothing nothing2 = " "
estadd local Controls "X", replace
estadd local CFE "X", replace
get_mean

esttab using "3_output/1_main_paper/Tables/Table3.tex", depvar keep(satis_head) ///
	order(eval_eco eval_sant serious_h_csqc serious_e_csqc) label replace wrap ///
	cells(b(star fmt(%15.3fc)) se(par fmt(%15.3fc))) interaction(" $/times$ ") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(Controls CFE N av IV fstat , layout(@ @ @ @ @ @ ) fmt(%15s %15s %15.0fc %9.3f %9.3f %9.3f ) ///
	labels("Individual controls" "Country FE" Observations "Outcome mean" "Instruments" "F-statistic" )) ///
	collabels(none) mlabels(none) ///
	mgroups("Satisfaction with democracy" , pattern(1 0 0 0) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
			
		
**# Table 4: Impact on support for democratic ideals 

eststo clear
/* 4. Col 1: Strong leader with 16 instruments */	
* to get the Cragg-Donald statistic: 
eststo: ivreg2 strong_leader ///
	(satis_head = TH1_TE1 TH1_TE2 TH1_TE3 TH1_TE4 TH2_TE1 TH2_TE2 TH2_TE3 TH2_TE4 TH3_TE1 TH3_TE2 TH3_TE3 TH3_TE4 TH4_TE1 TH4_TE2 TH4_TE3) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local CFE "X", replace
estadd local Controls "X", replace
estadd local IV "16 IVs", replace
get_mean

/* 4. Col 2: Strong leader with 1 instrument */	
* to get the Cragg-Donald statistic: 
eststo: ivreg2 strong_leader ///
	(satis_head = treatc) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local CFE "X", replace
estadd local Controls "X", replace
estadd local IV "SumIV", replace
get_mean

/* 4. Col 3: Experts with 16 instruments */	
* to get the Cragg-Donald statistic: 

eststo: ivreg2 experts ///
	(satis_head = TH1_TE1 TH1_TE2 TH1_TE3 TH1_TE4 TH2_TE1 TH2_TE2 TH2_TE3 TH2_TE4 TH3_TE1 TH3_TE2 TH3_TE3 TH3_TE4 TH4_TE1 TH4_TE2 TH4_TE3) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local CFE "X", replace
estadd local IV "16 IVs", replace
estadd local Controls "X", replace
get_mean

/* 4. Col 4: Experts with 1 instrument */	
* to get the Cragg-Donald statistic: 
eststo: ivreg2 experts ///
	(satis_head = treatc ) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local CFE "X", replace
estadd local IV "SumIV", replace
estadd local Controls "X", replace
get_mean

/* 4. Col 5: Army with 16 instruments*/	
* to get the Cragg-Donald statistic: 
eststo: ivreg2 army ///
	(satis_head = TH1_TE1 TH1_TE2 TH1_TE3 TH1_TE4 TH2_TE1 TH2_TE2 TH2_TE3 TH2_TE4 TH3_TE1 TH3_TE2 TH3_TE3 TH3_TE4 TH4_TE1 TH4_TE2 TH4_TE3) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local CFE "X", replace
estadd local IV "16 IVs", replace
estadd local Controls "X", replace
get_mean

/* 4. Col 6: Army with 1 instrument*/	
* to get the Cragg-Donald statistic: 
eststo: ivreg2 army ///
	(satis_head = treatc) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local CFE "X", replace
estadd local IV "SumIV", replace
estadd local Controls "X", replace
get_mean

/* 4. Col 7: Democracy with 16 instruments*/	
* to get the Cragg-Donald statistic: 
eststo: ivreg2 democ ///
	(satis_head = TH1_TE1 TH1_TE2 TH1_TE3 TH1_TE4 TH2_TE1 TH2_TE2 TH2_TE3 TH2_TE4 TH3_TE1 TH3_TE2 TH3_TE3 TH3_TE4 TH4_TE1 TH4_TE2 TH4_TE3) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local IV "16 IVs", replace
estadd local CFE "X", replace
estadd local Controls "X", replace
get_mean

/* 4. Col 8: Democracy with 1 instrument*/	
* to get the Cragg-Donald statistic: 
eststo: ivreg2 democ ///
	(satis_head = treatc ) $controls $mv_controls  i.country, small robust 

estadd scalar fstat = e(widstat)
estadd local IV "SumIV", replace
estadd local CFE "X", replace
estadd local Controls "X", replace
get_mean

esttab using "3_output/1_main_paper/Tables/Table4.tex", depvar keep(satis_head) ///
	label replace wrap ///
	cells(b(star fmt(%15.3fc)) se(par fmt(%15.3fc))) interaction(" $/times$ ") ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(Controls CFE N av IV fstat , layout(@ @ @ @ @ @) fmt(%15s %15s %15.0fc %12.3f %9.3f %9.3f ) ///
	labels("Individual controls" "Country FE" Observations "Outcome mean" "Instruments" "F-statistic" )) ////
	collabels(none) mlabels(none) ///
	mgroups("Strong leader" "Experts" "Army" "Democracy" , pattern(1 0 1 0 1 0 1 0 ) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
